<template>
	<view>
		<view class="userinfo">
			<image class="userinfo-avatar" background-size="cover"></image>
			<text class="userinfo-nickname"></text>
		</view>

		<view>
			<view>
				<view class="login-title">登录</view>
				<view class="login">
					<view class="first-line">
						<input placeholder-class="phcolor" class="tui-input" placeholder="请输入手机号码" maxlength="11"
							v-model="mobile" type='number' />
					</view>
					<view class="first-line">
						<input placeholder-class="phcolor" password class="tui-input" placeholder="登录密码" maxlength="8"
							v-model="password" />
					</view>
				</view>
				<view class="third-line">
					<!-- <tui-button shape="circle" type="danger" @click="startLogin">登录</tui-button> -->
					<button shape="circle" type="danger" @click="startLogin">登录</button>
					
					<button shape="circle" type="danger" @tap="getUserProfile" style=" margin-top: 10rpx;">快捷登录</button>
					
					<!-- <button class="btn btn_primary" open-type="getUserInfo" @getuserinfo="bindGetUserInfo">
						快捷登录2
					</button> -->
					
				</view>
				<view class="form-item-text">
					<navigator url="/pages/auth/mobile/mobile?type=2" class="reset">忘记密码</navigator>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	const util = require("@/utils/util.js")
	export default {
		data() {
			return {
				mobile: '',
				password: ''
			}
		},
		onLoad() {
			console.log("..............................onLoad..............................");
			var that = this
			if (!uni.getStorageSync('token')) {
				console.log("1111111111111111111token..........");
				uni.login({
					success: function(res) {
						if (res.code) {
							util.request('repairauth/' + res.code).then(res => {
								if (res.code === 0) {
									//存储用户信息
									uni.setStorageSync('userInfo', res.userInfo);
									uni.setStorageSync('token', res.token);
									uni.setStorageSync('userId', res.userId);
									that.userId = res.userId
									uni.reLaunch({
										url: '/pages/index/index'
									});
								} else {
									util.toast(res.msg)
								}
							});
						}
					}
				});
			}else {
				console.log("token...............")
				uni.reLaunch({
					url: '/pages/index/index'
				});
			}
			
		},
		methods: {
			startLogin: function() {
				let that = this;
				if (!util.isMobile(that.mobile)) {
					return
				}

				if (that.password.length < 1) {
					util.toast('请输入密码');
					return false;
				}
				util.request('auth/loginByMobile', {
					mobile: that.mobile,
					password: that.password
				}, 'POST').then(function(res) {
					if (res.code === 0) {
						//存储用户信息
						uni.setStorageSync('userInfo', res.userInfo);
						uni.setStorageSync('token', res.token);
						uni.setStorageSync('userId', res.userInfo.id);
						uni.reLaunch({
							url: '/pages/ucenter/index/index'
						});
					} else {
						util.toast(res.msg);
					}
				});
			},
			getUserProfile() {
				console.log(".....................")
				let that = this;
				//先同意协议
				// if (!that.checkIsTcp()) {
				// 	return
				// }
				// 推荐使用uni.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
				// 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
				uni.getUserProfile({
					desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: (res) => {
						//登录远程服务器
						if (that.code) {
							that.LoginByMa(res)
						} else {
							uni.login({
								success: function(resp) {
									if (resp.code) {
										that.code = resp.code
										that.LoginByMa(res)
									}
								}
							});
						}
					}
				})
			},
			LoginByMa(userInfo, type) {
				console.log("LoginByMa..................");
				let that = this;
				let url = 'repairauth/LoginByMa'
				// APP端微信登录
				if (type == 2) {
					url = 'repairauth/AppLoginByWx'
				}
				if (type == 3) {
					url = 'repairauth/LoginByAli'
					that.code = userInfo.authCode
				}
				util.request(url, {
					code: that.code,
					userInfo: userInfo
				}, 'POST', 'application/json').then(res => {
					if (res.code === 0) {
						//存储用户信息
						uni.setStorageSync('userInfo', res.userInfo);
						uni.setStorageSync('token', res.token);
						uni.setStorageSync('userId', res.userId);
				
						uni.switchTab({
							url: '/pages/index/index'
						})
						
						// if (type == 2 || type == 3) {
						// 	if (that.navUrl && (that.navUrl == '/pages/index/index' || that.navUrl ==
						// 			'/pages/ucenter/index/index' || that.navUrl == '/pages/cart/cart')) {
						// 		uni.switchTab({
						// 			url: that.navUrl
						// 		})
						// 	} else if (that.navUrl) {
						// 		uni.redirectTo({
						// 			url: that.navUrl,
						// 		})
						// 	}
						// }
						// #ifdef MP-WEIXIN
						// if (res.userInfo.mobile) {
						// 	if (that.navUrl && (that.navUrl == '/pages/index/index' || that.navUrl ==
						// 			'/pages/ucenter/index/index' || that.navUrl == '/pages/cart/cart')) {
						// 		uni.switchTab({
						// 			url: that.navUrl
						// 		})
						// 	} else if (that.navUrl) {
						// 		uni.redirectTo({
						// 			url: that.navUrl,
						// 		})
						// 	}
						// } else {
						// 	that.phoneVisible = true
						// }
						// #endif
					} else {
						util.toast(res.msg)
					}
				});
				this.code = ''
			},
		}
	}
</script>

<style lang="scss">
	.userinfo {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-bottom: 25rpx;
		background: linear-gradient(to bottom, $color-default 0%, #fff 100%);
		padding-top: 100rpx;
	}

	.userinfo-avatar {
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
		margin-top: 20rpx;
		margin-bottom: 25rpx;
	}

	.userinfo-nickname {
		color: #272727;
		font-size: 28rpx;
		line-height: 40rpx;
	}

	/* 绑定手机号的两个form */

	.login-title {
		margin: 20rpx 0 35rpx;
		text-align: center;
		font-size: 30rpx;
	}

	.login {
		font-size: 32rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.login .first-line {
		height: 80rpx;
		border: 1rpx solid rgb(217, 217, 217);
		border-radius: 40rpx;
		width: 600rpx;
		margin-bottom: 40rpx;
	}

	.login .first-line input {
		padding-left: 20rpx;
		height: 80rpx;
		width: 100%;
	}

	.third-line {
		margin-left: auto;
		margin-right: auto;
		width: 600rpx;
	}

	.third-line button {
		height: 80rpx;
		font-size: 32rpx;
		background-color: $color-default;
		color: #fff;
	}

	.form-item-text {
		float: right;
		margin: 40rpx 80rpx 0 0;
		color: $color-default;
		font-size: 22rpx;
	}
</style>